<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>根据指定格式解析输入的字符</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.sprintf.html">« sprintf</a></li>
      <li style="float: right;"><a href="function.str-contains.html">str_contains »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.strings.html">字符串 函数</a></li>
    <li>根据指定格式解析输入的字符</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.sscanf" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sscanf</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">sscanf</span> &mdash; <span class="dc-title">根据指定格式解析输入的字符</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.sscanf-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sscanf</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter reference">&...$vars</code></span>): <span class="type"><span class="type">array</span>|<span class="type">int</span>|<span class="type">null</span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>sscanf()</strong></span> 函数输入类似 <span class="function"><a href="function.printf.html" class="function">printf()</a></span>。<span class="function"><strong>sscanf()</strong></span>
   读取字符串 <code class="parameter">string</code>，然后根据指定格式 <code class="parameter">format</code> 解析。
  </p>
  <p class="para">
   指定的格式字符串中的任意空白匹配输入字符串的任意空白。也就是说即使是格式字符串中的一个制表符（<code class="literal">\t</code>）也能匹配输入字符串中的单个空白字符。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.sscanf-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       将要被解析的 <span class="type">string</span>。
      </p>
     </dd>

    
    

     <dt>
<code class="parameter">format</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">string</code> 的格式解释，在 <span class="function"><a href="function.sprintf.html" class="function">sprintf()</a></span> 的文档中进行了描述，但有以下区别：
       <ul class="simplelist">
        <li class="member">
         函数不支持区域设置。
        </li>
        <li class="member">
         不支持 <code class="literal">F</code>、<code class="literal">g</code>、<code class="literal">G</code> 和 <code class="literal">b</code>。
        </li>
        <li class="member">
         <code class="literal">D</code> 代表十进制数字。
        </li>
        <li class="member">
         <code class="literal">i</code> stands for integer with base detection.
        </li>
        <li class="member">
         <code class="literal">n</code> 代表到目前为止处理的字符数。
        </li>
        <li class="member">
         <code class="literal">s</code> 在任何空白字符处停止阅读。
        </li>
        <li class="member">
         <code class="literal">*</code> 代替 <code class="literal">argnum$</code> 禁止分配此转换规范。
        </li>
       </ul>
      </p>
     </dd>

    

    
     <dt>
<code class="parameter">vars</code></dt>

     <dd>

      <p class="para">
       可选参数将以引用方式传入，它们的值将被设置为解析值。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.sscanf-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果仅传入了两个参数给这个函数，解析后的值作为数组返回。否则，如果传入可选参数，函数将返回赋值数量。可选参数必须通过引用传递。
  </p>
  <p class="para">
   如果 <code class="parameter">format</code> 要求的子字符串比 <code class="parameter">string</code>
   内可用的子字符串多，则返回 <strong><code>null</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.sscanf-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4817">
    <p><strong>示例 #1 <span class="function"><strong>sscanf()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;获取序列号<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$serial</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">sscanf</span><span style="color: #007700">(</span><span style="color: #DD0000">"SN/2350001"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SN/%d"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;和生产日期<br /></span><span style="color: #0000BB">$mandate&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"January&nbsp;01&nbsp;2000"</span><span style="color: #007700">;<br />list(</span><span style="color: #0000BB">$month</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$day</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">sscanf</span><span style="color: #007700">(</span><span style="color: #0000BB">$mandate</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"%s&nbsp;%d&nbsp;%d"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Item&nbsp;</span><span style="color: #0000BB">$serial</span><span style="color: #DD0000">&nbsp;was&nbsp;manufactured&nbsp;on:&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #DD0000">-"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$month</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"-</span><span style="color: #0000BB">$day</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   如果传递可选参数，函数将返回赋值的数量。
  </p>
  <p class="para">
   <div class="example" id="example-4818">
    <p><strong>示例 #2 <span class="function"><strong>sscanf()</strong></span>——使用可选参数</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;获取作者信息并生成&nbsp;DocBook&nbsp;实体<br /></span><span style="color: #0000BB">$auth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"24\tLewis&nbsp;Carroll"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$n&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sscanf</span><span style="color: #007700">(</span><span style="color: #0000BB">$auth</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"%d\t%s&nbsp;%s"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$id</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$first</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$last</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"&lt;author&nbsp;id='</span><span style="color: #0000BB">$id</span><span style="color: #DD0000">'&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;firstname&gt;</span><span style="color: #0000BB">$first</span><span style="color: #DD0000">&lt;/firstname&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;surname&gt;</span><span style="color: #0000BB">$last</span><span style="color: #DD0000">&lt;/surname&gt;<br />&lt;/author&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.sscanf-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - 输出格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - 返回格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.fprintf.html" class="function" rel="rdfs-seeAlso">fprintf()</a> - 将格式化后的字符串写入到流</span></li>
    <li class="member"><span class="function"><a href="function.vprintf.html" class="function" rel="rdfs-seeAlso">vprintf()</a> - 输出格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.vsprintf.html" class="function" rel="rdfs-seeAlso">vsprintf()</a> - 返回格式化字符串</span></li>
    <li class="member"><span class="function"><a href="function.vfprintf.html" class="function" rel="rdfs-seeAlso">vfprintf()</a> - 将格式化字符串写入流</span></li>
    <li class="member"><span class="function"><a href="function.fscanf.html" class="function" rel="rdfs-seeAlso">fscanf()</a> - 从文件中格式化输入</span></li>
    <li class="member"><span class="function"><a href="function.number-format.html" class="function" rel="rdfs-seeAlso">number_format()</a> - 以千位分隔符方式格式化一个数字</span></li>
    <li class="member"><span class="function"><a href="function.date.html" class="function" rel="rdfs-seeAlso">date()</a> - 格式化 Unix 时间戳</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>